<script type="text/javascript" src="/My97DatePicker/WdatePicker.js"></script>

<div class="span10" id="datacontent">
    <ul class="breadcrumb">
        <li><a href="/weixin/product/index">微信399</a> <span class="divider">/</span></li>
        <li class="active">产品列表</li>
    </ul>
    <ul class="nav nav-tabs">
        <li class="active">
            <a href="javascript:;" onclick="javascript:openall();">课表列表</a>
        </li>
        <li class="dropdown pull-right" style="display:flex;">
            <a class="dropdown-toggle" href="javascript:;" style="position: relative">
                <input type="file" onchange="upLoadData()"
                       style="opacity: 0;width: 50px;position: absolute;top: 0;left: 0" id="fileInput" name="file">
                导入
            </a>
            <a class="dropdown-toggle" href="javascript:;" onclick="addClass()">添加课表</a>
        </li>
    </ul>

    <form action="" id="checkPush" method="post">
        <table class="table table-hover">
            <thead>
            <tr>
                <th width="80">编号</th>
                <th>名称</th>
                <th>科目</th>
                <th>老师</th>
                <th>日期</th>
                <th>上课时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <?php
            foreach ($data as $v) {
                ?>
                <tr>
                    <td><?php echo $v['id'] ?></td>
                    <td><?php echo $v['name'] ?></td>
                    <td><?php echo $v['section'] ?></td>
                    <td><?php echo $v['teacher'] ?></td>
                    <td><?php echo $v['date'] ?></td>
                    <td><?php echo $v['startTime'] . '-' . $v['endTime'] ?></td>
                    <td>
                        <button id="buttonChange">
                            <a href="javascript:;"
                               onclick="upLoadItem1('<?php echo $v['id'] ?>','<?php echo $v['name'] ?>','<?php echo $v['section'] ?>','<?php echo $v['teacher'] ?>','<?php echo $v['date'] ?>','<?php echo $v['startTime'] ?>','<?php echo $v['endTime'] ?>')">修改</a>
                        </button>
                        <button id="buttonDelete">
                            <a href="javascript:;" onclick="deleteItem('<?php echo $v['id'] ?>')">删除</a>
                        </button>
                    </td>
                </tr>
                <?php
            }
            ?>
            </tbody>
        </table>
    </form>
    <div class="pagination pagination-right">
        <?php use yii\widgets\LinkPager; ?>
        <?php echo LinkPager::widget([
            'pagination' => $page,
        ]) ?>
    </div>
    <div class="addProp"
         style="width: 100%;height: 100%;position: fixed;top: 0;left: 0;z-index: 10;background: rgba(0,0,0,0.5);display: none;">
        <div style="width: 800px;height: 200px;background: white;position: absolute;top: 50%;left: 50%;transform: translate(-50%,-50%)">
            <table style="margin-top: 20px" class="table table-hover" id="txtTable">
                <thead>
                <tr>
                    <th>名称</th>
                    <th>科目</th>
                    <th>老师</th>
                    <th>日期</th>
                    <th>上课时间</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td><input type="text" value="" id="className" style="width: 100px"></td>
                    <td><input type="text" value="" id="classSection" style="width: 100px"></td>
                    <td><input type="text" value="" id="classTeacher" style="width: 100px"></td>
                    <td><input class="input-small Wdate" onclick="WdatePicker()" type="text" size="10" name="beginTime"
                               id="classTime"
                               value="" autocomplete="off"/>
                    </td>
                    <td>
                        <input class="input-small Wdate" onclick="WdatePicker({dateFmt:'HH:mm'})" type="text" size="10"
                               name="beginTime" id="startTime" value="" autocomplete="off"/> - <input
                                class="input-small Wdate"
                                onclick="WdatePicker({dateFmt:'HH:mm'})"
                                type="text" size="10"
                                name="endTime" value="" id="endTime"
                                autocomplete="off"/>
                    </td>
                </tr>
                </tbody>
            </table>
            <div style="display: flex;justify-content: center">
                <button id="closeButton" onclick="resetData()">取消</button>
                <button id="buttonAdd" style="margin-left: 20px;" onclick="addClassData()">确认</button>
                <button id="buttonUp" style="margin-left: 20px;" onclick="upLoadClassData()" data-id="">确认</button>
            </div>
        </div>
    </div>
</div>

<script>
    function upLoadData() {
        let url = new URL(window.location.href);
        let params = new URLSearchParams(url.search);
        let id = params.get('id');
        var formData = new FormData();
        var fileInput = $('#fileInput')[0]; // 获取文件输入元素
        var file = fileInput.files[0]; // 获取选中的文件

        if (file) {
            formData.append('file', file); // 将文件添加到FormData对象中
            $.ajax({
                url: '/import/upload-file', // 服务器端处理上传的脚本地址
                type: 'POST',
                data: formData,
                processData: false, // 告诉jQuery不要处理发送的数据
                contentType: false, // 告诉jQuery不要设置Content-Type请求头
                success: function (response) {
                    if (JSON.parse(response).code == 1) {
                        $.post('/weixin/product/import-class', {
                            file: JSON.parse(response).path,
                            id: id,
                        }, function (res) {
                            if (res.code == 1) {
                                window.location.reload()
                            }
                        }, 'json')
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                }
            });
        } else {
            alert('请选择一个文件！');
        }
    }

    function addClass() {
        $('#buttonAdd').show()
        $('#buttonUp').hide()
        $('.addProp').show()
    }

    //新增课表
    function addClassData() {
        let url = new URL(window.location.href);
        let params = new URLSearchParams(url.search);
        let id = params.get('id');
        $.post('/weixin/product/add-class', {
            productId: id,
            name: $('#txtTable #className').val(),
            section: $('#txtTable #classSection').val(),
            teacher: $('#txtTable #classTeacher').val(),
            date: $('#txtTable #classTime').val(),
            startTime: $('#txtTable #startTime').val(),
            endTime: $('#txtTable #endTime').val(),
        }, function (res) {
            if (res.code == 1) {
                window.location.reload()
            }
        }, 'json')
    }

    //修改回填
    function upLoadItem1(id, name, section, teacher, date, startTime, endTime) {
        $('#txtTable #className').val(name)
        $('#txtTable #classSection').val(section)
        $('#txtTable #classTeacher').val(teacher)
        $('#txtTable #classTime').val(date)
        $('#txtTable #startTime').val(startTime)
        $('#txtTable #endTime').val(endTime)
        $('#buttonAdd').hide()
        $('#buttonUp').show().attr('data-id', id)
        $('.addProp').show()
    }

    // 确认修改
    function upLoadClassData() {
        $.post('/weixin/product/update-class', {
            id: $('#buttonUp').attr('data-id'),
            name: $('#txtTable #className').val(),
            section: $('#txtTable #classSection').val(),
            teacher: $('#txtTable #classTeacher').val(),
            date: $('#txtTable #classTime').val(),
            startTime: $('#txtTable #startTime').val(),
            endTime: $('#txtTable #endTime').val(),
        }, function (res) {
            if (res.code == 1) {
                window.location.reload()
            }
        }, 'json')
    }

    // 删除
    function deleteItem(id) {
        if (window.confirm('删除操作不可恢复！确认删除吗？')) {
            window.location.href = '/weixin/product/delete-class?id=' + id
        }
    }
    //重置
    function resetData() {
        $('#txtTable #className').val('')
        $('#txtTable #classSection').val('')
        $('#txtTable #classTeacher').val('')
        $('#txtTable #classTime').val('')
        $('#txtTable #startTime').val('')
        $('#txtTable #endTime').val('')
        $('.addProp').hide()
    }
</script>

